﻿#include "pch.h"

WoHTTPOutgoingResponsePtr WEBOAT_API WoCreateResponseFast(int code,const char*data) {
//	auto m=oatpp::web::protocol::http::outgoing::ResponseFactory::createResponse(oatpp::web::protocol::http::Status::CODE_200, "Hello, World!");
	return oatpp::web::protocol::http::outgoing::ResponseFactory::createResponse(
		oatpp::web::protocol::http::Status((v_int32)code, ""), data);
};

void WEBOAT_API WoResponsePutHeader(WoHTTPOutgoingResponsePtr& resp,const char* Key,const char*value) {
	resp->putHeader(Key,value);
}
CXTextA WEBOAT_API WoResponseGetHeader(WoHTTPOutgoingResponsePtr& resp, const char* Key) {
	auto rptr = resp->getHeader(Key);
	return rptr ? rptr->c_str() : "";
}
void WEBOAT_API WoResponseGetHeaders(WoHTTPOutgoingResponsePtr& resp, CXMap<CXTextA, CXTextA>& rmap) {
	auto headers = resp->getHeaders();
	auto ahds = headers.getAll();
	rmap.clear();
	for (auto i = ahds.begin(); i != ahds.end(); i++) {
		rmap[i->first.toString().getPtr()->c_str()] = i->second.toString().getPtr()->c_str();
	}
}

void WEBOAT_API WoResponseBodyAddBuffer(WoHTTPOutgoingResponsePtr& resp, const char* data, UINT size) {
	if (!data) { return; };
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	body->m_buffer.getPtr()->append(data, size);
	//body->AddByte(data, size);
}
void WEBOAT_API WoResponseBodyAddTextA(WoHTTPOutgoingResponsePtr& resp, const char* data) {
	if (!data) { return; };
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	body->m_buffer.getPtr()->append(data);
	//body->AddTextA(data);
}
void WEBOAT_API WoResponseBodySetBuffer(WoHTTPOutgoingResponsePtr& resp, const char* data, UINT size) {
	if (!data) { return; };
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	body->m_buffer.getPtr()->clear();
	body->m_buffer.getPtr()->append(data, size);
	//body->SetByte(data, size);
}
void WEBOAT_API WoResponseBodySetTextA(WoHTTPOutgoingResponsePtr& resp, const char* data) {
	if (!data) { return; };
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	body->m_buffer.getPtr()->clear();
	body->m_buffer.getPtr()->append(data);
	//body->SetTextA(data);
}
size_t WEBOAT_API WoResponseBodySize(WoHTTPOutgoingResponsePtr& resp) {
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	return body->m_buffer.getPtr()->size();
}
void WEBOAT_API WoResponseSendJsonByTemplate(WoHTTPOutgoingResponsePtr& resp,WoTemplateJson& json) {
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	resp->putHeader("Content-Type","application/json");
	body->m_buffer.getPtr()->clear();
	body->m_buffer.getPtr()->append(json.dump());
}



void WEBOAT_API WoResponseSendJsonByModel(WoHTTPOutgoingResponsePtr& resp, const char*modelName,void* modelPtr) {
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	resp->putHeader("Content-Type", "application/json");
	inja::json rJson;
	WoRefModelToJson(rJson, modelName,modelPtr);
	body->m_buffer.getPtr()->clear();
	body->m_buffer.getPtr()->append(rJson.dump());
}
void WEBOAT_API WoResponseSendByteAsync(WoHTTPOutgoingResponsePtr& resp,const void*data,int len) {
	//auto os = std::make_shared<oatpp::data::stream::BufferOutputStream>();
	//oatpp::async::Action act;
	//os->write(data, len, act);

	//resp->sendAsync(resp, os,);

}

int WEBOAT_API WoResponseGetStatus(WoHTTPOutgoingResponsePtr& resp) {
	return resp->getStatus().code;
}

void WEBOAT_API WoResponseGetData(WoHTTPOutgoingResponsePtr& resp,CXBytes&ret) {
	ret.clear();
	WoOutgoingBufferBody* body = (WoOutgoingBufferBody*)resp->getBody().get();
	auto ptr = body->m_buffer.get();
	if (!ptr) {
		ret = "";
		return;
	}
	ret.add((void*)(ptr->data()), ptr->size());
}
